package kernel.process;

import java.util.ArrayList;
import java.util.List;

import kernel.ProcessMemory;
import kernel.exception.OSException;

/**
 * Class responsible for starting new programs.
 * @author Petr Hajek
 */
public class ProcessCreator extends AbstractProcess {
	
	public ProcessCreator(String name, ArrayList<String> params, String input, String output, boolean append, long id) {
		super(name, params, input, output, append, id, "root", 0);
	}

	@Override
	/**
	 * {@inheritDoc}
	 */
	public String doJob(String input) throws OSException {
		while(!shouldEnd) {
			try {
				List<AbstractProcess> ap = ProcessMemory.takeFromNewPQ();
				ProcessDispatcher pd = new ProcessDispatcher(ap, getNewID());
				ProcessMemory.addProcess(pd);
				pd.start();
			} catch (InterruptedException e) {
				ProcessMemory.deleteProcess(0);
				return null;
			}
		}
		return null;
	}

	@Override
	/**
	 * {@inheritDoc}
	 */
	public void kill() throws OSException {
	}
	
	/**
	 * Generates an ID for a new process dispatcher.
	 * @return
	 */
	public long getNewID() {
		return ProcessMemory.dispCounter++;
	}

	@Override
	/**
	 * {@inheritDoc}
	 */
	public boolean needParams() {
		return false;
	}

	@Override
	/**
	 * {@inheritDoc}
	 */
	public String continueJob() throws OSException {
		shouldEnd = true;
		return null;
	}

	@Override
	/**
	 * {@inheritDoc}
	 */
	protected String finalizeJob() throws OSException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	/**
	 * {@inheritDoc}
	 */
	public String man(String command) {
		return "";
	}
}
